import tkinter
import opconfig
from screeninfo import get_monitors

mousexys=[0,0,0,0]
mousegeotext=0
mouserect=0
topwnd=tkinter.Tk()
topwnd.config(bg='red')
# 不显示最大化、最小化按钮
topwnd.overrideredirect(True)
topwnd.attributes('-alpha', 0.7)
def esc(event):
    topwnd.quit()
# 屏幕尺寸
mon=get_monitors()
can=tkinter.Canvas(topwnd,bg='black',width=mon[0].width,height=mon[0].height)
print("------canvas width",mon[0].width,"height",mon[0].height)
def mov(event):
    global mousegeotext
    can.delete(mousegeotext)
    mousegeotext =can.create_text(
        event.x, event.y, fill='red', text=str(event.x) + "+" + str(event.y),anchor='se')
def b1down(event):
    global mousexys
    mousexys[0]=event.x
    mousexys[1]=event.y
def b1mov(event):
    global mousexys
    global mousegeotext
    global mouserect
#    print(mousexys)
    mousexys[2]=event.x
    mousexys[3]=event.y
    can.delete(mousegeotext)
    can.delete(mouserect)
    mouserect =can.create_rectangle(mousexys, fill='white')
    mousegeotext =can.create_text(
        event.x, event.y, fill='red',anchor='se',
        text=str(mousexys[2]-mousexys[0])+"x"+str(mousexys[3]-mousexys[1])+"+"
             +str(mousexys[0]) + "+" + str(mousexys[1]))
def b3down(event):
    can.delete(mouserect)
can.configure(borderwidth=0, highlightthickness=0)
#键盘绑定到canvas无效，貌似因为焦点问题，只能绑到wnd上
topwnd.bind('<Escape>', esc)
can.bind('<Motion>',mov)
can.bind('<Button-1>', b1down)
can.bind('<B1-Motion>',b1mov)
can.bind('<Button-3>', b3down)
can.pack()

opconfig.load()
colors=['yellow','green','blue']
for i in range(1,opconfig.rectnum+1):
    xys=[]
    geo=opconfig.gets(i)
    opconfig.geotoxy(geo,xys)
    can.create_rectangle(xys,fill=colors[(i-1)%3],outline='')
    can.create_text(xys[0],xys[1],text=str(i),fill='red',font=('Arial',15,'bold'),anchor='nw')
    can.create_text(xys[0],xys[1]+30,text=geo,fill='red',font=('Arial',10,),anchor='nw')
    can.create_text(100,0,text="按住鼠标左键拖动显示框选区域geometry参数，右键重新框选。按ESC退出。",
                    fill='red',font=('Arial',20,'bold'),anchor='nw')
    #print(xys)
topwnd.mainloop()